package logic

import (
	"context"
	"user-membership/rpc/goods"

	"user-membership/api/internal/svc"
	"user-membership/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddOrderLogic) AddOrder(req *types.AddOrderRequest) (resp *types.Response, err error) {
	userId := req.UserId
	if userId == 0 {
		return &types.Response{
			Code: -1,
			Msg:  "用户不存在",
			Data: nil,
		}, nil
	}
	res, err := l.svcCtx.GoodsSrv.CreateOrder(l.ctx, &goods.CreateOrderReq{
		UserId:  int64(req.UserId),
		GoodsId: int64(req.GoodsId),
		Payment: int64(req.Payment),
		Num:     req.Num,
	})
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "添加订单失败",
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 0,
		Msg:  "添加订单成功",
		Data: map[string]interface{}{
			"PayUrl": res.PayUrl,
			"UserId": req.UserId,
		},
	}, nil
}
